<script setup>
  // 表单相关
  import { reactive, ref, shallowRef } from 'vue'
  import { adminLogin } from '@/api/adminUser.js'
  import { setStorage } from '@/utils/storage.js'
  import { useRouter } from 'vue-router'

  // 在vue3中需要使用useRouter来获取路由实例
  const $router = useRouter()

  const form = reactive({ mobile: '18974618496', password: 'abc123123' })
  const rules = shallowRef({
    mobile: [{ message: '手机号不能为空', required: true, trigger: 'blur' }],
    password: [{ message: '密码不能为空', required: true, trigger: 'blur' }]
  })
  const formRef = ref(null)

  const login = async () => {
    try {
      await formRef.value.validate()
      // 校验成功后，执行登录操作
      const { data } = await adminLogin(form)
      // 保存用户信息到本地
      setStorage('adminUser', data)
      // 跳转到首页
      $router.push('/')
    } catch (e) {
      console.log('校验失败')
    }
  }
</script>

<template>
  <div class="login-view">
    <div class="login-main">
      <h1 class="title">在线旅游后台管理</h1>
      <el-form ref="formRef" :model="form" :rules="rules">
        <el-form-item class="login-input" prop="mobile">
          <el-input clearable placeholder="你的手机号" v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item class="login-input" prop="password">
          <el-input type="password" show-password clearable placeholder="你的密码" v-model="form.password"></el-input>
        </el-form-item>
      </el-form>
      <el-button class="login-btn" color="#67c23c" @click="login">登录</el-button>
    </div>
  </div>
</template>

<style lang="scss">
  .login-view {
    width: 100%;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-image: url('https://diancan-1252107261.cos.accelerate.myqcloud.com/lvyou/icon/login-pc-bei.jpg');
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
    .login-main {
      width: 550px;
      height: 340px;
      box-sizing: border-box;
      padding: 30px 100px;
      background: #fff;
      border-radius: 20px;
      .title {
        font-size: 30px;
        font-weight: 700;
        text-align: center;
        background-color: inherit !important;
        color: #67c23a;
      }
      .login-input {
        margin: 20px 0;
        div {
          background-color: #f6f7f9;
        }
      }
      .login-btn {
        margin: 20px 0;
        width: 350px;
        color: #fff;
      }
    }
  }
</style>
